package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Behospital;
import com.kkb.service.BehospitalService;
import com.kkb.utils.ResultUtil;
import com.kkb.vo.BehospitalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author:CoolTiger
 * @title: BehospitalController
 * @projectName hospital1.0
 * @description: TODO
 * @date:2022/07/24/14:54:59
 **/
@Controller
@ResponseBody
@RequestMapping("/hospital/behospital")
public class BehospitalController {
    @Autowired
    private BehospitalService behospitalService;

    /**
     * TODO 住院信息：分页+多条件查询
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultUtil<Behospital> queryAll(Integer pageNum, Integer pageSize, BehospitalVo vo){
        //1.判断分页数据是否为空
        if(pageNum == null || pageNum <= 0){
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0){
            pageSize = 5;
        }
        //2.查询并返回数据
        PageInfo<Behospital> behospitalPageInfo = behospitalService.queryAll(pageNum, pageSize, vo);
        return new ResultUtil<Behospital>(behospitalPageInfo);
    }

    /**
     * TODO 添加住院信息
     * @param behospital
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultUtil<Behospital> addBehospital(Behospital behospital){
        //1.添加住院数据
        int i = behospitalService.addBehospital(behospital);
        //2.判断是否成功，反馈信息
        if (i == 1){
            return new ResultUtil<>();
        }
        return new ResultUtil<>(500,"添加失败！");
    }

    /**
     * TODO 通过id退院
     * @param behospitalId
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public ResultUtil<Behospital> deleteBehospital(@PathVariable("id") Integer behospitalId){
        //1.退院
        int i = behospitalService.deleteBehospital(behospitalId);
        //2.判断是否成功，反馈信息
        if (i == 1){
            return new ResultUtil<>();
        }
        return new ResultUtil<>(500,"退院失败！");
    }

    /**
     * TODO 通过id查询住院信息
     * @param behospitalId
     * @return
     */
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public ResultUtil<Behospital> queryById(@PathVariable("id") Integer behospitalId){
        //1.拿id查询数据
        Behospital behospital = behospitalService.queryById(behospitalId);
        //2.判断查询是否成功，反馈信息
        if (behospital != null){
            return new ResultUtil<>(200,"查询成功");
        }
        return new ResultUtil<>(500,"查询失败！");

    }
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public ResultUtil<Behospital> updateBehospital(@PathVariable("id") Integer behospitalId,Behospital behospital){
        //1.封装id
        behospital.setBehId(behospitalId);
        //2.更新
        int i = behospitalService.updateBehospital(behospital);
        //3.判断更新是否成功，反馈信息
        if (i == 1){
            return new ResultUtil<>(200,"更新成功");
        }
        return new ResultUtil<>(500,"退院失败！");
    }
}
